<!--
 * @Author: [yuantongxin]
 * @Date: 2021-10-23 16:36:53
 * @LastEditors: [yuantongxin]
 * @LastEditTime: 2021-10-25 18:41:17
 * @Description: 
-->
<template>
  <div>
    <!-- 按钮开始 -->
    <div>
      <el-button type="primary" @click="toAdd">新增</el-button>
    </div>
    <!-- 按钮结束 -->

    <!-- 表格开始 -->
    <div>
      <!-- {{ tableData }} -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" :index="1" label="序号" width="220">
        </el-table-column>
        <el-table-column prop="name" label="名称" width="220">
        </el-table-column>
        <el-table-column prop="no" label="排序号" width="220">
        </el-table-column>
        <el-table-column prop="description" label="描述" width="220">
        </el-table-column>
        <el-table-column label="操作" width="220">
          <template slot-scope="scope">
            <el-button type="text" size="small" icon="el-icon-edit" @click="toEdit(scope.row)">编辑</el-button>
            <el-button type="text" size="small" icon="el-icon-delete" style="color: #e86e7d" @click="toDelete(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 表格结束 -->
    <!-- 模态框开始 -->
    <div>
      <el-dialog :title="title" :visible.sync="visible">
        <el-form :model="form" :rules="rules" ref="myForm">
          <el-form-item
            label="栏目名称"
            :label-width="formLabelWidth"
            prop="name"
          >
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="序号" :label-width="formLabelWidth">
            <el-input v-model="form.no" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="栏目介绍" :label-width="formLabelWidth">
            <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入内容"
              v-model="form.description"
            >
            </el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="visible = false">取 消</el-button>
          <el-button type="primary" @click="toSubmit('myForm')">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <!-- 模态框结束 -->
  </div>
</template>

<script>
import { findAll,saveOrUpdate,deleteById } from "@/api/website";
export default {
  data() {
    return {
      rules: {
        name: [
          { required: true, message: "请输入栏目名称", trigger: "blur" },
          // { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
      },
      tableData: [],
      form: {},
      formLabelWidth: "120px",
      title: "",
      visible: false,
      //   description: ''
    };
  },
  created() {
    this.finded();
    // this.findAll();
  },
  methods: {
    async finded() {
      let res = await findAll();
      console.log(res);
      this.tableData = res.data;
    },
    toAdd() {
      this.visible = true;
      this.title = "新增栏目信息";
      this.form = {};
    },
    toEdit(row){
        this.visible = true;
        this.title = '修改栏目信息'
        this.form = {...row}
    },
    toDelete(id) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res = await deleteById({ id });
          this.finded();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    toSubmit(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          let res = await saveOrUpdate(this.form);
          this.visible = false;
          console.log( '提交',res);
          this.$message({
            message: res.message,
            type: "success"
          });
          // 刷新页面
          this.finded();
          // alert("submit!");
        } else {
          this.$message.error('保存失败');
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
</style>